import Vue from 'vue'
import Router from 'vue-router'

// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)
const constantRouterMap = [
  {
    path: '/',
    component: () => import('@/views/layouts/index'),
    redirect: '/home',
    meta: {
      title: '首页',
      keepAlive: false
    },
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import(/* webpackChunkName: "home" */ '@/views/home/index'),
        meta: { title: '首页', keepAlive: false }
      },
      {
        path: '/radio',
        name: 'Radio',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/questionBank/radio'),
        meta: { title: '单选题', keepAlive: true }
      },
      {
        path: '/multipleChoice',
        name: 'Multiplechoice',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/questionBank/multipleChoice'),
        meta: { title: '多选题', keepAlive: false }
      },
      {
        path: '/determine',
        name: 'Determine',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/questionBank/determine'),
        meta: { title: '判断题', keepAlive: false }
      },
      {
        path: '/databaseRadio',
        name: 'Databaseradio',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/database/radio'),
        meta: { title: '数据库单选题', keepAlive: true }
      },
      {
        path: '/databaseMultipleChoice',
        name: 'Databasemultiplechoice',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/database/multipleChoice'),
        meta: { title: '数据库多选题', keepAlive: false }
      },
      {
        path: '/databaseDetermine',
        name: 'Databasedetermine',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/database/determine'),
        meta: { title: '数据库判断题', keepAlive: false }
      },
      {
        path: '/middlewareradio',
        name: 'Middlewareradio',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/Middleware/radio'),
        meta: { title: '中间件单选题', keepAlive: true }
      },
      {
        path: '/middlewaremultiplechoice',
        name: 'MiddlewareMultipleChoice',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/Middleware/multipleChoice'),
        meta: { title: '中间件多选题', keepAlive: false }
      },
      {
        path: '/middlewaredetermine',
        name: 'Middlewaredetermine',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/Middleware/determine'),
        meta: { title: '中间件判断题', keepAlive: false }
      },
      {
        path: '/safetyRegulationsradio',
        name: 'Safetyregulationsradio',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/safetyRegulations/radio'),
        meta: { title: '安规单选题', keepAlive: true }
      },
      {
        path: '/safetyRegulationsmultiplechoice',
        name: 'Safetyregulationsmultiplechoice',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/safetyRegulations/multipleChoice'),
        meta: { title: '安规多选题', keepAlive: false }
      },
      {
        path: '/safetyRegulationsDetermine',
        name: 'Safetyregulationsdetermine',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/safetyRegulations/determine'),
        meta: { title: '安规件判断题', keepAlive: false }
      },
      {
        path: '/simulatedExam',
        name: 'Simulatedexam',
        component: () => import(/* webpackChunkName: "chart" */ '@/views/questionBank/simulatedExam'),
        meta: { title: '模拟考试', keepAlive: false }
      }
      // {
      //   path: '/chart',
      //   name: 'Chart',
      //   component: () => import(/* webpackChunkName: "chart" */ '@/views/chart/index'),
      //   meta: { title: '图表', keepAlive: false }
      // },
      // {
      //   path: '/form',
      //   name: 'Form',
      //   component: () => import(/* webpackChunkName: "form" */ '@/views/form'),
      //   meta: { title: '搜索', keepAlive: false }
      // },
      // {
      //   path: '/about',
      //   name: 'About',
      //   component: () => import(/* webpackChunkName: "home" */ '@/views/home/about'),
      //   meta: { title: '关于我', keepAlive: false }
      // }
    ]
  }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath
    // base: process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
